#include "apnavtreemodel.h"
#include <QBrush>

ApNavTreeModel::ApNavTreeModel(QObject *parent) :
    QAbstractItemModel(parent),
    mp_dbms(NULL)
{
}


void ApNavTreeModel::load_data()
{
    if(mp_dbms == NULL) return;
    mqvl_str_indexes = mp_dbms->get_db(LAKDB_STREETS)->findDataRange(FLD_STDB_NAME);
    mqvl_str_indexes.removeFirst();
    QVariantList qvl_hs_indexes;
    QVariant qv_s_index;
    mqvl_hs_indexes_lst.clear();

    for(int i = 0; i<mqvl_str_indexes.size(); i++)
    {
        qv_s_index  =  mqvl_str_indexes.at(i);
        qvl_hs_indexes = mp_dbms->get_db(LAKDB_HOUSES)->findDataEq(FLD_HSDB_STINDEX,qv_s_index);
        mqvl_hs_indexes_lst.append(qvl_hs_indexes);
    }
    qDebug()<<"ap tree model loaded ";
    qDebug()<<"streets loaded"<<mqvl_str_indexes.size();
}

void ApNavTreeModel::set_dbms(HousingDBMS *p_dbms)
{
    mp_dbms = p_dbms;
}

QModelIndex ApNavTreeModel::index(int row, int column, const QModelIndex &parent) const
{
    if(!parent.isValid())
    {
        return createIndex(row,column,row);
    }
    if(parent.row() < 1000)
       return  createIndex(row, column, parent.row()+1000);
    return QModelIndex();
}

QModelIndex ApNavTreeModel::parent(const QModelIndex &child) const
{
    if(!child.isValid()) return QModelIndex();
    if(child.internalId() < 1000) return QModelIndex();
    return createIndex((int)child.internalId()-1000, 0, (quint32)child.internalId()-1000);
}


int ApNavTreeModel::rowCount(const QModelIndex &parent) const
{
    if(!parent.isValid())
    {
        return mqvl_str_indexes.size();
    }
    if(parent.internalId() > 999)
    {
        return 0;
    }
    return  mqvl_hs_indexes_lst.at(parent.row()).size();
}

int ApNavTreeModel::columnCount(const QModelIndex &parent) const
{
    if(!parent.isValid()) return 2;
    if(parent.internalId()<1000) return 2;
    if(parent.internalId()>999) return 0;
    return 0;
}

QVariant  ApNavTreeModel::data(const QModelIndex &index, int role) const
{
    if(role == Qt::BackgroundRole)
    {

        if(index.internalId() < 1000)
        {
            if(index.row()%2)
                return QVariant(QBrush(QColor(192,220,192)));
            else
                return QVariant(QBrush(QColor(180,230,180)));
        }
    }

    if(role != Qt::DisplayRole) return QVariant();
    QString str;
    QVariant qv_key;
    SLEE_HDB_REC qh_data;
    if(index.internalId() < 1000)
    {
        if(index.column() == 0)
        {
            qv_key = mqvl_str_indexes.at(index.row());
            str = mp_dbms->get_street_info(qv_key);
            return QVariant(str);
        }
        if(index.column() ==1)
        {
            str += QString::number(mqvl_hs_indexes_lst.at(index.row()).size());
            str += RUSTR(" домов");
            return QVariant(str);
        }
    }
    int s_index;
    s_index = index.internalId()-1000;
    qv_key = mqvl_hs_indexes_lst.at(s_index).at(index.row());
    qh_data = mp_dbms->get_data(LAKDB_HOUSES, qv_key);

    if(index.column()==0)
    {
        str = mp_dbms->get_house_info(qh_data);
        return QVariant(str);
    }
    QStringList qstrl_fac = mp_dbms->get_facilities_list( qh_data.value(FLD_HSDB_FACILITIES).toInt(), true );
    int fac_size = qstrl_fac.size();
    for(int j = 0; j < fac_size; j++)
    {
        str+=qstrl_fac.at(j);
        if(j < (fac_size-1)) str+=", ";
    }
    return QVariant( str);
}


int ApNavTreeModel::get_nav_type(const QModelIndex &index)
{
    if(index.internalId()<1000) return 0;
    return 1;
}

QVariant ApNavTreeModel::get_db_index(const QModelIndex &index)
{
    if(index.internalId() < 1000)
        return mqvl_str_indexes.at(index.row());
    return mqvl_hs_indexes_lst.at(index.internalId()-1000).at(index.row());
}

QModelIndex ApNavTreeModel::createIndexFor(int type, const QVariant &qv_key)
{
    int row;
    if(type == IN_STREETS)
    {
        row = mqvl_str_indexes.indexOf(qv_key);
        if(row == -1) return QModelIndex();
        return createIndex(row,0,row);
    }
    if(type==IN_HOUSES)
    {
        for(int si = 0; si < mqvl_str_indexes.size(); si++)
        {
           row = mqvl_hs_indexes_lst.at(si).indexOf(qv_key);
           if(row == -1) continue;
           return createIndex(row, 0, si+1000);
        }
    }
    return QModelIndex();
}


QVariantList ApNavTreeModel::get_h_indexes()
{
    QVariantList qvl_ret;
    for(int i = 0; i< mqvl_hs_indexes_lst.size(); i ++)
    {
        qvl_ret.append(mqvl_hs_indexes_lst.at(i));
    }
    return qvl_ret;
}


QVariant ApNavTreeModel::get_h_index(const QModelIndex &index)
{
    int s_row = index.internalId();
    if(s_row < 1000)
    {
        if(mqvl_hs_indexes_lst.at(s_row).isEmpty()) return QVariant();
        return mqvl_hs_indexes_lst.at(s_row).at(0);
    }
    else
    {
        s_row -=1000;
        return mqvl_hs_indexes_lst.at(s_row).at(index.row());
    }
    return QVariant();
}

